import moment from 'moment'
export default {
	data() {
		return {
			flag: true,
			now: moment(),
			endDate: '',
		}
	},
	computed: {
		countDown() {
			return function(examDuration) {
				if (this.flag) {
					this.endDate = Date.now() + 60 * 1000 * examDuration
				}
				this.flag = false
				let m1 = this.now
				let m2 = moment(this.endDate)
				var du = moment.duration(m2 - m1, 'ms'),
					hours = du.get('hours'),
					mins = du.get('minutes'),
					ss = du.get('seconds')
				if (hours <= 0 && mins <= 0 && ss <= 0) {
					return '00:00:00'
				} else {
					let time =
						this.PrefixInteger(hours, 2) +
						':' +
						this.PrefixInteger(mins, 2) +
						':' +
						this.PrefixInteger(ss, 2)
					this.timeNotice(hours, mins, ss)
					return time
				}
			}
		},
	},
	mounted() {
		setInterval(() => {
			this.now = moment()
		}, 1000)
	},
	methods: {
		PrefixInteger(num, n) {
			return (Array(n).join(0) + num).slice(-n)
		},
		timeNotice(h, m, s) {
			if (h <= 0 && m == 1 && s == 0) {
				this.$notify({
					title: '提示',
					message: '您的考试时间不多了，请尽快答题',
					type: 'warning',
				})
			}
		},
	},
}